package com.yyjj.hangqing.activity;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.aries.ui.view.title.TitleBarView;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.vise.xsnow.event.BusManager;
import com.yyjj.hangqing.R;
import com.yyjj.hangqing.adapter.CacheAdatpter;
import com.yyjj.hangqing.adapter.EditCacheAdatpter;
import com.yyjj.hangqing.cache.CoinCache;
import com.yyjj.hangqing.entity.CacheEntity;

import java.util.Iterator;

import tech.com.commoncore.base.BaseTitleActivity;

@Route(path = "/hq/edite")
public class EditCoinActitivy extends BaseTitleActivity {
    RecyclerView rv_content;
    private RecyclerView rvContent;
    private TextView text;
    private LinearLayout allLayout;
    private ImageView qxImg;
    private LinearLayout deleteLayout;
    boolean isChoos;
    EditCacheAdatpter cacheAdatpter;


    @Override
    public void setTitleBar(TitleBarView titleBar) {
        titleBar.setTitleMainText("编辑自选").setOnLeftTextClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

    }

    @Override
    public int getContentLayout() {
        return R.layout.hq_edit_activity;
    }

    @Override
    public void initView(Bundle savedInstanceState) {
        cacheAdatpter = new EditCacheAdatpter();
        cacheAdatpter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                if (cacheAdatpter.getData().get(position).isChoose()) {
                    cacheAdatpter.getData().get(position).setChoose(false);
                } else {
                    cacheAdatpter.getData().get(position).setChoose(true);
                }

                cacheAdatpter.notifyDataSetChanged();
            }
        });
        rvContent = findViewById(R.id.rv_content);
        rvContent.setLayoutManager(new LinearLayoutManager(mContext));
        rvContent.setAdapter(cacheAdatpter);
        cacheAdatpter.replaceData(CoinCache.initCoinCache().getList(mContext));
        text = findViewById(R.id.text);
        allLayout = findViewById(R.id.all_layout);
        qxImg = findViewById(R.id.qx_img);
        deleteLayout = findViewById(R.id.delete_layout);
        deleteLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Iterator<CacheEntity> entityIterator = cacheAdatpter.getData().iterator();
                while (entityIterator.hasNext()) {
                    if (entityIterator.next().isChoose()) {
                        entityIterator.remove();
                    }
                }
                CoinCache.initCoinCache().setCodeList(cacheAdatpter.getData());
                CoinCache.initCoinCache().saveData(mContext);
                BusManager.getBus().post(new CacheEntity());
                cacheAdatpter.notifyDataSetChanged();
                if (cacheAdatpter.getData().size() == 0) {
                    text.setVisibility(View.VISIBLE);
                }
            }
        });
        mContentView.findViewById(R.id.all_layout).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (cacheAdatpter.getData().size() == 0) {
                    return;
                }
                if (isChoos) {
                    isChoos = false;
                    mContentView.findViewById(R.id.qx_img).setBackgroundResource(R.mipmap.hq_optional_icon_all);
                } else {
                    mContentView.findViewById(R.id.qx_img).setBackgroundResource(R.mipmap.hq_optional_icon_choose);
                    isChoos = true;

                }
                for (CacheEntity entity : cacheAdatpter.getData()) {
                    if (isChoos) {
                        entity.setChoose(true);
                    } else {
                        entity.setChoose(false);
                    }
                }
                cacheAdatpter.notifyDataSetChanged();

                // CoinCache.initCoinCache().setCodeList();
            }
        });


    }
}
